package com.sskj.mine.ui.activity;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gamerole.orcameralib.CameraActivity;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.IdCardVerification;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.http.HttpConfig;
import com.sskj.mine.presenter.CertificationActivityPresenter;

import java.io.File;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

@Route(path = RConfig.MINE_CERTIFICATION)//实名认证
public class CertificationActivity extends BaseActivity<CertificationActivityPresenter> {
    @BindView(R2.id.etName)
    EditText etName;
    @BindView(R2.id.etIDCard)
    EditText etIDCard;
    @BindView(R2.id.ivCardFront)
    ImageView ivCardFront;
    @BindView(R2.id.ivCardBack)
    ImageView ivCardBack;
    @BindView(R2.id.ivCardHolder)
    ImageView ivCardHolder;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    private static final int REQUEST_A_CAMERA = 1000;
    private static final int REQUEST_B_CAMERA = 1001;
    private static final int REQUEST_C_CAMERA = 1002;
    private static final int REQUEST_D_CAMERA = 1003;
    private static final String NAME_A = "photo_a.jpg";
    private static final String NAME_B = "photo_b.jpg";
    private static final String NAME_C = "photo_c.jpg";
    private static final String NAME_D = "photo_d.jpg";
    @BindView(R2.id.tvCardType)
    TextView tvCardType;
    @BindView(R2.id.llCardType)
    LinearLayout llCardType;
    @BindView(R2.id.llIDCard)
    LinearLayout llIDCard;
    @BindView(R2.id.ivPassport)
    ImageView ivPassport;
    @BindView(R2.id.llPassport)
    LinearLayout llPassport;
    private int card;
    private String mCurrFileName;
    @Inject
    UserViewModel userViewModel;
    private String idCardFrontImg;
    private String idCardBackImg;
    private String selfieImg;
    private int cardType = 0;
    private BottomSheetDialog cardBottom;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_certification;
    }

    @Override
    public CertificationActivityPresenter getPresenter() {
        return new CertificationActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.mineCertificationActivity3));
        DaggerUserDataComponent.create().inject(this);
        ClickUtil.click(ivCardFront, () -> {
            mCurrFileName = NAME_A;
            Intent intent = new Intent(this, CameraActivity.class);
            intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    getSaveFile(getApplication(), NAME_A).getAbsolutePath());
            intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_FRONT);
            startActivityForResult(intent, REQUEST_A_CAMERA);
        });
        ClickUtil.click(ivCardBack, () -> {
            mCurrFileName = NAME_B;
            Intent intent = new Intent(this, CameraActivity.class);
            intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    getSaveFile(getApplication(), NAME_B).getAbsolutePath());
            intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_BACK);
            startActivityForResult(intent, REQUEST_B_CAMERA);
        });
        ClickUtil.click(ivCardHolder, () -> {
            mCurrFileName = NAME_C;
            Intent intent = new Intent(this, CameraActivity.class);
            intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    getSaveFile(getApplication(), NAME_C).getAbsolutePath());
            intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intent, REQUEST_C_CAMERA);
        });
        ClickUtil.click(ivPassport, () -> {
            mCurrFileName = NAME_D;
            Intent intent = new Intent(this, CameraActivity.class);
            intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    getSaveFile(getApplication(), NAME_D).getAbsolutePath());
            intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intent, REQUEST_D_CAMERA);
        });
        ClickUtil.click(llCardType, () -> {
            if (cardBottom == null) {
                cardBottom = BottomSheetUtil.getBottomSheet(this, "选择", (recyclerView, position, v) -> {
                    cardBottom.dismiss();
                    if (cardType != position) {
                        cardType = position;
                        changeType();
                    }
                }, "身份证", "护照");
            }
            cardBottom.show();
        });

        ClickUtil.click(btSubmit, () -> {
            String identNum = etIDCard.getText().toString().trim();
            String realName = etName.getText().toString().trim();

            if (!realName.matches("^[A-Za-z\\u4e00-\\u9fa5]+$")) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineCertificationActivity5));
                return;
            }
            if (TextUtils.isEmpty(realName)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineCertificationActivity6));
                return;
            }
            if (TextUtils.isEmpty(identNum)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineCertificationActivity7));
                return;
            }
            if (cardType == 0) {//身份证
                if (!IdCardVerification.IDCardValidate(identNum).equals(IdCardVerification.VALIDITY)) {
                    ToastUtil.showShort(IdCardVerification.IDCardValidate(identNum));
                    return;
                }
            } else {
                if (!CheckUtil.isPassport(identNum)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mineCertificationActivity4));
                    return;
                }
            }
            if (cardType == 0) {
                if (TextUtils.isEmpty(idCardBackImg) || TextUtils.isEmpty(idCardFrontImg) || TextUtils.isEmpty(selfieImg)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mineCertificationActivity9));
                    return;
                }
            } else {
                if (TextUtils.isEmpty(selfieImg)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mineCertificationActivity9));
                    return;
                }
            }
//            if (TextUtils.isEmpty(idCardBackImg) || TextUtils.isEmpty(idCardFrontImg) || TextUtils.isEmpty(selfieImg)) {
//                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineCertificationActivity9));
//                return;
//            }
//            mPresenter.verify(realName, identNum, idCardFrontImg, idCardBackImg, selfieImg, "", "");
            mPresenter.realNameVerify(identNum, realName, cardType + 1, idCardFrontImg, idCardBackImg, selfieImg);
        });
    }

    private void changeType() {
        if (cardType == 0) {//身份证
            llPassport.setVisibility(View.GONE);
            llIDCard.setVisibility(View.VISIBLE);
            tvCardType.setText("身份证");
            etIDCard.setHint("请输入身份证号码");
        } else {
            llPassport.setVisibility(View.VISIBLE);
            llIDCard.setVisibility(View.GONE);
            tvCardType.setText("护照");
            etIDCard.setHint("请输入护照号码");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case REQUEST_A_CAMERA://身份证正面
                    card = 0;
                    mPresenter.uploadImg(new File(getFilesDir(), NAME_A));
                    break;
                case REQUEST_B_CAMERA://身份证背面
                    card = 1;
                    mPresenter.uploadImg(new File(getFilesDir(), NAME_B));
                    break;
                case REQUEST_C_CAMERA: //人证合一
                    card = 2;
                    mPresenter.uploadImg(new File(getFilesDir(), NAME_C));
                    break;
                case REQUEST_D_CAMERA: //护照
                    card = 3;
                    mPresenter.uploadImg(new File(getFilesDir(), NAME_D));
                    break;
            }
        }
    }

    private File getSaveFile(Context context, String failName) {
        File file = new File(context.getFilesDir(), failName);
        return file;
    }


    public void onSubmitVerifyResponseSuccess() {
//        ToastUtil.showShort(App.INSTANCE.getString(R.string.mineCertificationActivity10));
        userViewModel.update();
        finish();
    }

    public void updatePath(String path) {
        switch (card) {
            case 0:
                idCardFrontImg = path;
                ImageUtil.setImage(HttpConfig.BASE_URL + path, ivCardFront);
                break;
            case 1:
                idCardBackImg = path;
                ImageUtil.setImage(HttpConfig.BASE_URL + path, ivCardBack);
                break;
            case 2:
                selfieImg = path;
                ImageUtil.setImage(HttpConfig.BASE_URL + path, ivCardHolder);
                break;
            case 3:
                selfieImg = path;
                ImageUtil.setImage(HttpConfig.BASE_URL + path, ivPassport);
                break;
        }
    }
}
